#include once "mud-gui/util.bi"

namespace ui

	extern _MainWidget_ as BaseWidget ptr

	sub ConsolePrint( byref _logstr_ as string )

		static ff as integer = 0
		ff = freefile

		open cons for output as #ff
		print #ff, _logstr_
		close #ff

	end sub

	sub LogPrint( byref _logstr_ as string, byref fil_ as string = "" )

		static ff as integer = 0
		static fil as string
		ff = freefile
		if fil_ = "" then fil = command(0) & ".log"

		open fil for append as #ff
		print #ff, date() & " " & time() & " " & _logstr_
		close #ff

	end sub


	function boundingBox( byval x as integer, byval y as integer, _
											byval w as integer, byval h as integer, _
											byval m_x as integer, byval m_y as integer ) _
											as integer

		function = 0

		if m_x > x and m_x < (x + w) then
			if m_y > y and m_y < (y + h) then function = 1
		EndIf

	end function

	function initScreen( byref title as string = "", byval w as integer = 320, byval h as integer = 240, _
				byval b as integer = 32, byval flags as integer = 0 ) _
				as integer

	screenres w, h, b, 1, flags
	if title <> "" then windowtitle title
	return iif( screenptr, 1, 0 )

	end function

	function SendEvent overload ( byval event_ as _GUI_EVENT_ ptr ) as integer
		return _MainWidget_->process_event( _MainWidget_, event_ )
	end function

	function SendEvent ( byval w as BaseWidget ptr, byval event_ as _GUI_EVENT_ ptr ) as integer
		return w->process_event( w, event_ )
	end function

	function SendUserEvent overload ( byval _data as any ptr ) as integer
		var e = new _GUI_EVENT_
		e->id = gevent.user
		e->user = _data
		function = SendEvent( e )
		delete e
	end function

	function SendUserEvent ( byval w as BaseWidget ptr, byval _data as any ptr ) as integer
		var e = new _GUI_EVENT_
		e->id = gevent.user
		e->user = _data
		function = SendEvent( w, e )
		delete e
	end function

	sub ShowError( byref _error_ as string )
		cls
		print "Error: " & _error_
		print
		print "Ending program"
		sleep
		end 32
	end sub

End Namespace
